package online.yysh.charging;

import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import online.yysh.charging.base.BaseActivity;
import online.yysh.charging.bean.request.FeedbackRequest;
import online.yysh.charging.utils.HttpUtil;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;


public class FeedbackActivity extends BaseActivity {
    private TextView tv_cancel,tv_title,tv_count;
    private EditText et_feedback;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_feedback;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        et_feedback = findViewById(R.id.et_feedback);
        et_feedback.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                tv_count.setText("已输入"+s.length()+"/100");
            }
        });
        tv_cancel = findViewById(R.id.tv_cancel);
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tv_title = findViewById(R.id.tv_title);
        tv_title.setText(R.string.feedback);
        tv_count = findViewById(R.id.tv_count);
    }

    @Override
    protected void initData() {

    }

    public void feedback(View view){
        String feedback = et_feedback.getText().toString().trim();

        if (TextUtils.isEmpty(feedback)){
            showToast("反馈信息为空");
            return;
        }
        String systemVersion = "Android版本："+getSystemVersion();
        String systemModel = "手机型号："+getSystemModel();
        logE("getSystemVersion:"+getSystemVersion()+"--getSystemModel:"+getSystemModel());
        FeedbackRequest request = new FeedbackRequest(openid,systemModel+";"+systemVersion+";"+feedback);
        HttpUtil.getInstance().feedback(request, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
                showToast("反馈失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                logE("feedback:"+result);
                if (result.contains("ok")){
                    showToast("反馈成功");
                    finish();
                }else{
                    showToast("反馈失败");
                }
            }
        });
    }

    /**
     * 获取系统版本号
     * @return
     */
    private String getSystemVersion(){
        return Build.VERSION.RELEASE;
    }

    /**
     * 获取手机型号
     * @return
     */
    private String getSystemModel(){
        return Build.MODEL;
    }

}
